<?php

namespace app\data\logic;

use app\data\service\ProductsFieldsService;
use Exception;

class ProductsLogic extends BaseLogic
{
    protected ProductsFieldsService $productsFieldsService;

    public function __construct()
    {
        $this->productsFieldsService = new ProductsFieldsService();
    }

    /**
     * 查询产品字段列表
     * @param $params
     * @return mixed
     */
    public function getProductsFieldsList($params)
    {
        $field = '*';
        $pageIndex = $params['page'] ?? 1;
        $pageSize = $params['limit'] ?? 10;
        $where = [];
        if (array_key_exists('keywords', $params) && $params['keywords']) {
            $keywords = $params['keywords'];
            $where[] = function ($query) use ($keywords) {
                $query->whereLike('name', '%' . $keywords . '%');
            };
        }
        $withs = ['productsCategoryList'];
        $order = 'sort desc,create_time desc';
         $result = $this->productsFieldsService->getPageList($pageIndex, $pageSize, $where, $field, $order,$withs);
        if (array_key_exists('all', $params)) {
            $result['list'] = $this->productsFieldsService->getAll($field, $where, $order,$withs);
            $result['count'] = $this->productsFieldsService->total($where);
        }

        return $result;
    }


    /**
     * 添加产品字段
     * @param $data
     * @return array
     */
    public function createNewProductsFields($data): array
    {
        try {
            $ret = $this->productsFieldsService->insert($data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 更新产品字段
     * @param $id
     * @param $data
     * @return array
     */
    public function updateProductsFields($id, $data): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $ret = $this->productsFieldsService->update($where, $data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 产品字段详情
     * @param $id
     * @return array
     */
    public function getProductsFieldsDetail($id): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $withs = ['productsCategoryList'];
            $res = $this->productsFieldsService->getOne('*', $where, $withs);
            if ($res) {
                return $this->responseInfo(true, 'SUCCESS', $res);
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 删除产品字段
     * @param $id
     * @return array
     */
    public function deleteProductsFields($id): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $ret = $this->productsFieldsService->softDel($where);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

}